package com.tanhua.mongo.api;

import com.tanhua.api.service.CommentApi;
import com.tanhua.model.enums.CommentType;
import com.tanhua.model.mongo.Comment;
import com.tanhua.model.mongo.Movement;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;


@DubboService
public class CommentApiImpl implements CommentApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public Integer saveComment(Comment comment) {

        // 保存数据
        mongoTemplate.save(comment);

        // 更新动态表中的字段
        Query query = new Query(Criteria.where("id").is(comment.getPublishId()));

        // 更新的数据
        Update update = new Update();
        if (comment.getCommentType() == CommentType.COMMENT.getType()) {
            // 更新评论数据
            update.inc("commentCount", 1);
        } else if (comment.getCommentType() == CommentType.LOVE.getType()) {
            // 更新喜欢数据
            update.inc("loveCount", 1);
        } else {
            update.inc("likeCount", 1);
        }

        // 设置更新参数
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true);  // 获取更新后的数据

        Movement modify = mongoTemplate.findAndModify(query, update, options, Movement.class);

        return modify.statisCount(comment.getCommentType());
    }

    @Override
    public List<Comment> findPageComment(Integer page, Integer pagesize, String movementId) {
        return mongoTemplate.find(
                new Query(Criteria
                        .where("publishId").is(new ObjectId(movementId))
                        .and("commentType").is(CommentType.COMMENT.getType()))
                    .skip((page - 1) * pagesize)
                    .limit(pagesize)
                    .with(Sort.by(Sort.Order.desc("created"))), Comment.class);
    }

    @Override
    public Boolean hasComment(String movementId, Long userId, CommentType like) {
        return mongoTemplate.exists(new Query(Criteria
                .where("userId").is(userId)
                .and("publishId").is(new ObjectId(movementId))
                .and("commentType").is(like.getType())
        ),Comment.class);
    }

    @Override
    public Integer remove(Comment comment) {

        // 删除点赞数据
        mongoTemplate.remove(new Query(Criteria
                .where("publishId").is(comment.getPublishId())
                .and("commentType").is(comment.getCommentType())
                .and("userId").is(comment.getUserId())
        ), Comment.class);

        // 更新动态数表中点赞，喜欢，品论的数量
        Query query = new Query(Criteria.where("id").is(comment.getPublishId()));

        Update update = new Update();
        if (comment.getCommentType() == CommentType.COMMENT.getType()) {
            // 评论
            update.inc("commentCount", -1);
        } else if (comment.getCommentType() == CommentType.LIKE.getType()) {
            // 点赞
            update.inc("likeCount", -1);
        } else {
            // 喜欢
            update.inc("loveCount", -1);
        }
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true);

        Movement modify = mongoTemplate.findAndModify(query, update, options, Movement.class);

        return modify.statisCount(comment.getCommentType());
    }
}
